package com.cloud.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloud.commonapi.jsonResult.Result;
import com.cloud.commonapi.model.*;

import com.cloud.order.config.order;
import com.cloud.order.service.BusinessOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * @author 陈楚城
 * @date 2019/9/30 15:07
 */
@RestController
@RequestMapping("/order/businessorders")
@Api(tags = "跑腿业务订单接口")
public class businessOrderController {

   @Autowired
    private BusinessOrderService businessOrderService;
    @Autowired
    private RestTemplate restTemplate;
   @PostMapping("")
   @ApiOperation(value ="跑腿订单生成",notes ="未测试" )
    public Result addorder( Business_order business_order, HttpSession session){

       //查看用户的信誉积分
       JSONObject jSONObject= restTemplate.getForObject("http://usercenter/usercontroller/userinfo/users/"+(Integer) session.getAttribute("loginId"),JSONObject.class);
       JSONObject  data=jSONObject.getJSONObject("data");
       User_info user_info=JSON.parseObject(data.toString(), User_info.class);
       System.out.println(user_info.getUsername());
       if(user_info.getCredit()<60){
           return Result.build(400,"您的信誉不合格，请快去实名吧！");
       }

       if(business_order.getAccountId().equals((Integer)session.getAttribute("loginId"))){
           return Result.build(400,"你不能接自己发布的业务");
       }
       business_order.setNumber(order.getnumber());
       business_order.setAccountId((Integer) session.getAttribute("loginId"));//登录人id，购买人id
       business_order.setStarttime(new Timestamp(new Date().getTime()));
       business_order.setStatus(0);

       Business_info business_info=new Business_info();
       business_info.setId(business_order.getBusinessId());
       business_info.setStatus(-1);
       if(businessOrderService.addorder(business_order) >0){
           //修改商品为已经售出
           restTemplate.put("http://business/business/update",business_info);

           return Result.build(200,"增加成功");
       }
       return Result.build(400,"增加失败");
   }
    @GetMapping("/{id}")
    @ApiOperation(value ="查看跑腿订单详情",notes ="未测试" )
    public Result getorder(@PathVariable Integer id, HttpSession session){
        Business_order business_order=businessOrderService.getorder(id);
        if(business_order!=null){
            return Result.buildData(202,"查询成功",business_order);
        }
        return Result.build(400,"查询失败");
    }

    @GetMapping("/list")
    @ApiOperation(value ="查看已接订单列表",notes ="未测试" )
    public Result getorderlist( HttpSession session){
        Integer accountId = (Integer) session.getAttribute("loginId");//登录人id
        //查看我接了什么订单
        List<Business_order> list=businessOrderService.list(accountId);
        List<Business_info> business_infos=new ArrayList<>();
        List<User_info> user_infos=new ArrayList<>();
        for(Business_order Business_order : list){
            //查看这些订单的业务信息
            JSONObject jSONObject1= restTemplate.getForObject("http://business/business/ByOrder/"+ Business_order.getBusinessId().intValue(),JSONObject.class);
            JSONObject  data1=jSONObject1.getJSONObject("data");
            Business_info business_info= JSON.parseObject(data1.toString(), Business_info.class);
            business_infos.add(business_info);
            //发布人
            JSONObject jSONObject= restTemplate.getForObject("http://usercenter/usercontroller/userinfo/users/"+business_info.getAccountId(),JSONObject.class);
            JSONObject  data=jSONObject.getJSONObject("data");
            User_info user_info= JSON.parseObject(data.toString(), User_info.class);
            user_infos.add(user_info);
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("business_infos",business_infos);
        map.put("user_infos",user_infos);
        if(map.size()>0){
            return Result.buildData(202,"查询成功",map);
        }
        return Result.build(400,"查询失败");
    }

    @GetMapping("sell/list")
    @ApiOperation(value ="查看被接订单列表",notes ="未测试" )
    public Result getorderlist1( HttpSession session){
        Integer accountId = (Integer) session.getAttribute("loginId");//登录人id
        JSONObject jSONObject= restTemplate.getForObject("http://business/business/ByOrder/accountId/"+accountId,JSONObject.class);

        JSONArray jsonArray=jSONObject.getJSONArray("data");
        if(jsonArray==null)
            return Result.build(400,"没有被接业务订单");
        String js=jsonArray.toJSONString();
        //找到我发布的业务
        List<Business_info> Business_info1= JSON.parseArray(js,Business_info.class);


        List<Business_order> orderlist =  new ArrayList<>();
        List<Business_info> business_infos = new ArrayList<>();
        for(Business_info business_info:Business_info1){
            //找到这些业务是否生成订单
            List<Business_order> businessOrders=businessOrderService.list1(business_info.getId());
            if(businessOrders.size()>0){
                orderlist.addAll(businessOrders);
                    //如果是，加入业务信息
                    business_infos.add(business_info);

            }
        }
        List<User_info> user_infos=new ArrayList<>();
        for(Business_order business_order:orderlist){
            //user_infos.add()
            //找到这些订单是谁接单的
            JSONObject jSONObject1= restTemplate.getForObject("http://usercenter/usercontroller/userinfo/users/"+business_order.getAccountId(),JSONObject.class);
            JSONObject  data=jSONObject1.getJSONObject("data");
            User_info user_info= JSON.parseObject(data.toString(), User_info.class);
            user_infos.add(user_info);
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("orderlist",orderlist);
        map.put("user_infos",user_infos);
        map.put("business_infos",business_infos);
        return Result.buildData(202,"查询成功",map);

    }

    @PutMapping("")
    @ApiOperation(value ="修改订单状态",notes ="未测试" )
    public Result updateorder(  Business_order business_order, HttpSession session){
        if(businessOrderService.updateorder(business_order)>0){
            return Result.build(200,"修改成功");
        }
        return Result.build(400,"修改失败");

    }

    @PutMapping("cancel")
    @ApiOperation(value ="取消订单",notes ="未测试" )
    public Result updateorder1(Business_order business_order, HttpSession session){

        if(businessOrderService.updateorder(business_order)>0){
            System.out.println(business_order.getBusinessId());
            Business_info business_info=new Business_info();
            business_info.setId(business_order.getBusinessId());
            business_info.setStatus(1);
            restTemplate.put("http://business/business/update",business_info);
            return Result.build(200,"修改成功");
        }
        return Result.build(400,"修改失败");

    }
}
